package com.beemindz.miyotee.provider;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

public class TaskHelper {

  private static final String[] TASK_PROJECTION = new String[] { MyToDo.Tasks._ID, MyToDo.Tasks.COLUMN_NAME_ID,
      MyToDo.Tasks.COLUMN_NAME_NAME, MyToDo.Tasks.COLUMN_NAME_DESCRIPTION, MyToDo.Tasks.COLUMN_NAME_REMINDER_DATE,
      MyToDo.Tasks.COLUMN_NAME_CREATE_DATE, MyToDo.Tasks.COLUMN_NAME_UPDATE_DATE, MyToDo.Tasks.COLUMN_NAME_USER_NAME,
      MyToDo.Tasks.COLUMN_NAME_IS_REMINDER, MyToDo.Tasks.COLUMN_NAME_IS_COMPLETE };

  private ContentResolver contentResolver;

  public TaskHelper(Context context) {
    this.contentResolver = context.getContentResolver();
  }

  /**
   * Get all task.
   * 
   * @return List task.
   */
  public ArrayList<Task> loadAllTask() {
    ArrayList<Task> tasks = new ArrayList<Task>();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    StringBuilder sortOrder = new StringBuilder();
    //sortOrder.append(MyToDo.Tasks.COLUMN_NAME_CREATE_DATE);
    //sortOrder.append(" , ");
    sortOrder.append(MyToDo.Tasks.COLUMN_NAME_UPDATE_DATE);
    sortOrder.append(" DESC ");

    Cursor cursor = contentResolver.query(MyToDo.Tasks.CONTENT_URI, TASK_PROJECTION,
        MyToDo.Tasks.COLUMN_NAME_IS_COMPLETE + " = ?",
        new String[] { "0" }, null);
    Cursor cursorComplete = contentResolver.query(MyToDo.Tasks.CONTENT_URI, TASK_PROJECTION,
        MyToDo.Tasks.COLUMN_NAME_IS_COMPLETE + " = ?",
        new String[] {"1"},
        sortOrder.toString());
    if (cursor.moveToFirst()) {
      while (!cursor.isAfterLast()) {
        int id = cursor.getInt(cursor.getColumnIndex(MyToDo.Tasks._ID));
        int idTask = cursor.getInt(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_ID));
        String nameTask = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_NAME));
        String desTask = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_DESCRIPTION));
        String reminderDate = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_REMINDER_DATE));
        String createdDate = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_CREATE_DATE));
        String updatedDate = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_UPDATE_DATE));
        String userName = cursor.getString(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_USER_NAME));
        boolean isReminder = cursor.getInt(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_IS_REMINDER)) == 0 ? false
            : true;
        boolean isComplete = cursor.getInt(cursor.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_IS_COMPLETE)) == 0 ? false
            : true;
        Task task = new Task(id, idTask, userName, nameTask, desTask, reminderDate, createdDate, updatedDate,
            isReminder,isComplete);
        tasks.add(task);
        cursor.moveToNext();
      }
    }

    if (cursorComplete.moveToFirst()) {
      while (!cursorComplete.isAfterLast()) {
        int id = cursorComplete.getInt(cursorComplete.getColumnIndex(MyToDo.Tasks._ID));
        int idTask = cursorComplete.getInt(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_ID));
        String nameTask = cursorComplete.getString(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_NAME));
        String desTask = cursorComplete.getString(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_DESCRIPTION));
        String reminderDate = cursorComplete.getString(cursorComplete
            .getColumnIndex(MyToDo.Tasks.COLUMN_NAME_REMINDER_DATE));
        String createdDate = cursorComplete.getString(cursorComplete
            .getColumnIndex(MyToDo.Tasks.COLUMN_NAME_CREATE_DATE));
        String updatedDate = cursorComplete.getString(cursorComplete
            .getColumnIndex(MyToDo.Tasks.COLUMN_NAME_UPDATE_DATE));
        String userName = cursorComplete.getString(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_USER_NAME));
        boolean isReminder = cursorComplete.getInt(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_IS_REMINDER)) == 0 ? false
            : true;
        boolean isComplete = cursorComplete.getInt(cursorComplete.getColumnIndex(MyToDo.Tasks.COLUMN_NAME_IS_COMPLETE)) == 0 ? false
            : true;

        Task task = new Task(id, idTask, userName, nameTask, desTask, reminderDate, createdDate, updatedDate,
            isReminder,isComplete);
        tasks.add(task);
        cursorComplete.moveToNext();
      }
    }

    return tasks;
  }

  /**
   * Delete a task.
   */
  public void deleteTask() {
  }
}
